<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>sojamo.http documentation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1>sojamo.http</h1>
<p>
sHTTP is a web server implementation for processing. (experimental stage). 
why a webserver  for processing? the main idea is to control processing through a web interface with rich graphical user interfaces. jQuery or prototype allow to convert a browser into a highly flexible controller interface. Using AJAX and JSON enables a direct and smooth communication between both environments. controlling a processing sketch from a single computer as well as from a remote computer makes it easy to customize and control a sketch on the fly.
</p>
<ul>
<li><a href="../reference/index.htm">sHTTP</a></li>
</ul>
<pre>



// experimental example using a processing sketch as a web server.
import sojamo.http.*;

HTTPServer h;
int bck = 0;
boolean debug;

void setup() {
  size(400,400);
  frameRate(30);
  // to use port 80 (the default port for http) you need to use sudo 
  // or you have to be logged in as root. instead lets use port 8080.
  h = new HTTPServer(this,8080);
  // the path to the directory where the browser can find
  // the requested files e.g. index.html etc.
  h.setRootPath(dataPath("www"));
  
  // a folder www lives inside your sketch's data folder
  // in here you can put your html, css, javascript, etc. files
  // which can be called from within a browser.
  
  // HTTPServer.DEBUG = true;
  // in browser use:
  
  // http://localhost:8080/index.html
  // http://localhost:8080/image.html
  
  // index.html and/or image.html must live inside
  // the www folder. see above.
  
}


void draw() {
  background(bck);
  bck -= 10;
  bck = max(0,bck);
}

void httpEvent(HTTPRequest theRequest) {
  // every request is replied to a browser by sending
  // a HTTPReply. you can create your own replies, now
  // a reply is sending back the requested file.
  // myReply.send("some text"); would send the characters "some text"
  // this can also be HTML formated text.
  
  HTTPReply myReply = new HTTPReply(theRequest);
  // send back requested file. if file is not found,
  // send an error message.
  if(theRequest.file().startsWith("get.cmd")) {
    //myReply.send("got a command "+theRequest.query("q"));
    bck = int(theRequest.query("q"));
    println(theRequest.query("q"));
  } else {
    // myReply.send(HTTPImage.convert(loadImage("data/www/test.jpg")), "jpg");
    println(theRequest.toString());
    println(java.net.URLDecoder.decode(theRequest.toString()));
    myReply.send();
    bck = 255;
  }
  if(debug) {
    println("> "+theRequest.decode()+" \n\n"+theRequest.report());
  }
}
  
</pre>
@libname sojamo/http
@documentationLocation sojamo/http
@templatesLocation ../
</body>
</html>
